<?php
/**
 * Desc: Service模板
 * User: zdb
 * Date: 2023/6/5
 * Time: 11:22
 */

namespace app\api\service\resource;
use app\util\DateUtil;
use think\facade\Db;

class LibMovieSubtitleService
{

    private $primaryTable;

    public function __construct()
    {
        $this->primaryTable = "yj_lib_movie_subtitle";
    }

    public function findRowById($id)
    {
        return Db::table($this->primaryTable)->alias('a')
            ->where('id', '=', $id)
            ->strict(false)
            ->find();
    }

    public function findRowByData($data)
    {
        $condition = $this->assembleFilterCondition($data);
        return Db::table($this->primaryTable)->alias('a')
            ->where($condition)
            ->strict(false)
            ->find();
    }

    public function getListByManyId($arrId)
    {
        $list = Db::table($this->primaryTable)->alias('a')
            ->whereIn('id', $arrId)
            ->select()
            ->toArray();
        return $list;
    }

    public function assembleFilterCondition($data)
    {
        $condition = ['a.status > 0'];

        if (!empty($data['subtitle'])) {
            $condition[] = "a.subtitle like '%" . $data['subtitle'] . "%'";
        }


        if (!empty($data['movie_id'])) {
            $condition[] = "a.movie_id =".$data['movie_id'];
        }

        if (!empty($data['seq_index'])) {
            $condition[] = "a.seq_index =".$data['seq_index'];
        }

        if (!empty($data['time_point_start'])) {
            if(is_array($data['time_point_start'])){
                $condition[] = "a.time_point_start in ('".implode("','",$data['time_point_start'])."') ";
            }else{
                $condition[] = "a.time_point_start = '".$data['time_point_start']."' ";
            }
        }

        if (!empty($data['subtitle_lang'])) {
            $condition[] = "a.subtitle_lang = '".$data['subtitle_lang']."' ";
        }

        if (!empty($data['spelling'])) {
            $condition[] = "a.subtitle like '%" . $data['spelling'] . "%'";
        }

        return empty($condition) ? "" : implode(" AND ", $condition);
    }

    public function getListNoPage($data = [], $limitNum = 30)
    {
        $condition = $this->assembleFilterCondition($data);
        $res = Db::table($this->primaryTable)->alias('a')
            ->where($condition)
            ->limit(0, $limitNum)
            ->order('a.id desc')
            ->select()
            ->toArray();
        return $res;
    }

    public function getListWithPage($data, $pageIndex = 1, $pageSize = 30)
    {
        $offset = ($pageIndex - 1) * $pageSize;
        $condition = $this->assembleFilterCondition($data);
        $res = Db::table($this->primaryTable)->alias('a')
            ->where($condition)
            ->limit($offset, $pageSize)
            ->order('a.id asc')
            ->select();
        return $res;
    }

    public function getTotalNum($data)
    {
        $condition = $this->assembleFilterCondition($data);
        $num = Db::table($this->primaryTable)->alias('a')
            ->where($condition)
            ->count();
        return $num;
    }

    public function createRow($data)
    {
        $data['status'] = isset($data['status']) ? $data['status']:1;
        $res = Db::table($this->primaryTable)
            ->strict(false)
            ->insert($data, true);
        return $res;
    }

    public function createManyRow($data)
    {
        $res = Db::table($this->primaryTable)
            ->strict(false)
            ->duplicate($data)
            ->insertAll($data, true);
        return $res;
    }

    public function updateRowById($id, $data)
    {
        $res = Db::table($this->primaryTable)->alias('a')
            ->where('id', '=', $id)
            ->strict(false)
            ->save($data);
        return false !== $res ? true : false;
    }

    public function updateListByData($condition, $data)
    {
        $condition = $this->assembleFilterCondition($data);
        $res = Db::table($this->primaryTable)->alias('a')
            ->where($condition)
            ->save($data);
        return false !== $res ? true : false;
    }

    public function updateListByManyId($arrId, $data)
    {
        $res = Db::table($this->primaryTable)->alias('a')
            ->whereIn('a.id', $arrId)
            ->strict(false)
            ->save($data);
        return false !== $res ? true : false;
    }

    public function removeRowById($id)
    {
        $data = ['status' => 0];
        $res = Db::table($this->primaryTable)
            ->where('id', '=', $id)
            ->strict(false)
            ->save($data);
        return false !== $res ? true : false;
    }

    public function removeListByManyId($arrId)
    {
        $data = ['status' => 0];
        $res = Db::table($this->primaryTable)->alias('a')
            ->whereIn('a.id', $arrId)
            ->strict(false)
            ->save($data);
        return false !== $res ? true : false;
    }

    public function removeListByData($data)
    {
        $condition = $this->assembleFilterCondition($data);
        $data = ['status' => 0];
        $res = Db::table($this->primaryTable)->alias('a')
            ->where($condition)
            ->save($data);
        return false !== $res ? true : false;
    }

    public function findMatchedSubtitle($movieId, $arrTime)
    {
        //根据时间，找到英文字幕对应的seq_index
        $arrStartTime = explode(",", $arrTime[0]);
        $arrEndTime = explode(",", $arrTime[1]);
//        echo "start time(his):".$arrStartTime[0]."<br>";
        $startSecondTime = DateUtil::getSecondTimeOfHisFormat($arrStartTime[0]);
//        echo "start time(second):".$startSecondTime."<br>";
        $startTime = $startSecondTime*1000 + intval($arrStartTime[1]);

        $endSecondTime = DateUtil::getSecondTimeOfHisFormat($arrEndTime[0]);
        $endTime = $endSecondTime*1000 + intval($arrEndTime[1]);

//        echo $startTime."--------".$endTime."<br>";
        //情形1：包含
        $info = Db::table($this->primaryTable)
            ->where('status', '=', 1)
            ->where('movie_id', '=', $movieId)
            ->where('start_time', '>=', $startTime)
            ->where('end_time', '<=', $endTime)
            ->find();
        if($info){
            return $info;
        }

        //情形2：被包含
        $info = Db::table($this->primaryTable)
            ->where('status', '=', 1)
            ->where('movie_id', '=', $movieId)
            ->where('start_time', '<=', $startTime)
            ->where('end_time', '>=', $endTime)
            ->find();
        if($info){
            return $info;
        }


        //情形3：右相交
        $info = Db::table($this->primaryTable)
            ->where('status', '=', 1)
            ->where('movie_id', '=', $movieId)
            ->where('start_time', '<=', $startTime)
            ->where('end_time', '>', $startTime)
            ->where('end_time', '<=', $endTime)
            ->where("end_time -".$startTime.">".$startTime."-start_time")   //确保台词时间匹配范围超过一半以上
            ->find();
        if($info){
            return $info;
        }

        //情形4：左相交
        $info = Db::table($this->primaryTable)
            ->where('status', '=', 1)
            ->where('movie_id', '=', $movieId)
            ->where('start_time', '>=', $startTime)
            ->where('start_time', '<', $endTime)
            ->where('end_time', '>=', $endTime)
//            ->where($endTime."-start_time > end_time -".$endTime)   //确保台词时间匹配范围超过一半以上
            ->find();
        if($info){
            return $info;
        }
        return array();

    }
}